﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Xaml.Behaviors;

namespace WinFrameApp
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class WinFrame : UserControl,INotifyPropertyChanged
    {
        StiZoomableCanvas MCancas;
        WindowState WinState = WindowState.Normal;
        double MWidth, MHeight;
        Point OffSet;
        bool IsMinWind = false;

        public event PropertyChangedEventHandler PropertyChanged;

        private void Pro(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
            }
        }


        #region 附件属性

        public ContentControl MContent
        {
            get { return (ContentControl)GetValue(MContentProperty); }
            set { SetValue(MContentProperty, value); }
        }
        
        public static readonly DependencyProperty MContentProperty =
            DependencyProperty.Register("MContentProperty", typeof(ContentControl), typeof(WinFrame), new PropertyMetadata(default(ContentControl), (d, e) =>
            {

                WinFrame user = d as WinFrame;

                user.grid.Children.Add(user.MContent);

            }));

        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("HeaderProperty", typeof(string), typeof(WinFrame), new PropertyMetadata(default(string), (d, e) =>
            {
                WinFrame user = d as WinFrame;
                user.title.Text = user.Header;

            }));

        public Visibility MinBtnVis
        {
            get { return (Visibility)GetValue(MinBtnVisProperty); }
            set { SetValue(MinBtnVisProperty, value); }

        }

        public static readonly DependencyProperty MinBtnVisProperty =
           DependencyProperty.Register("MinBtnVisProperty", typeof(Visibility), typeof(WinFrame), new PropertyMetadata(default(Visibility), (d, e) =>
           {
               WinFrame user = d as WinFrame;
               user.Minimize.Visibility = user.MinBtnVis;

           }));

        public Visibility MaxBtnVis
        {
            get { return (Visibility)GetValue(MaxBtnVisProperty); }
            set { SetValue(MaxBtnVisProperty, value); }

        }

        public static readonly DependencyProperty MaxBtnVisProperty =
           DependencyProperty.Register("MaxBtnVisProperty", typeof(Visibility), typeof(WinFrame), new PropertyMetadata(default(Visibility), (d, e) =>
           {
               WinFrame user = d as WinFrame;
               user.maxornormal.Visibility = user.MaxBtnVis;

           }));

        #endregion




        public WinFrame()
        {
            InitializeComponent();
            this.DataContext = this;
            this.Loaded += WinFrame_Loaded;        
          
        }

        bool Init = true;
        private void WinFrame_Loaded(object sender, RoutedEventArgs e)
        {
            if (Init) {
                Interaction.GetBehaviors(this).Add(new UserControlBehaviors());
                MCancas = this.Parent as StiZoomableCanvas;
                Init = false;
                maxornormal.Visibility = Visibility.Collapsed;
                Minimize.Visibility = Visibility.Collapsed;
            }
            
        }


        private void Maxornormal_Click(object sender, RoutedEventArgs e)
        {            
           
            if (WinState == WindowState.Normal)
            {
                if (!IsMinWind)
                {
                    MWidth = MCancas.Extent.Width;
                    MHeight = MCancas.Extent.Height;
                    OffSet = MCancas.Offset;
                }
                
                this.Width = MCancas.ActualWidth;
                this.Height = MCancas.ActualHeight;

                MCancas.Offset = new Point(0, 0);
                //Canvas.SetTop(this, 0.0);
                //Canvas.SetLeft(this, 0.0);

                WinState = WindowState.Maximized;
                restorebtn.Visibility = Visibility.Visible;
                maxornormal.Visibility = Visibility.Collapsed;

                
            }
            else
            {
                this.Width = MWidth;
                this.Height = MHeight;
                MCancas.Offset = OffSet;
                WinState = WindowState.Normal;
                restorebtn.Visibility = Visibility.Collapsed;
                maxornormal.Visibility = Visibility.Visible;

            }

            IsMinWind = false;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {           
            MCancas.Children.Remove(this);
        }

        private void Min_Click(object sender, RoutedEventArgs e)
        {
            Grid grid=MCancas.Parent as Grid;
            
            if (WinState == WindowState.Normal)
            {
                if (!IsMinWind)
                {
                    MWidth = MCancas.Extent.Width;
                    MHeight = MCancas.Extent.Height;
                    OffSet = MCancas.Offset;
                    WinState = WindowState.Minimized;
                }                              
            }
            else if(WinState == WindowState.Maximized)
            {
                WinState = WindowState.Normal;
            }
            else
            {
                return;
            }

            IsMinWind = true;

            int n=grid.Children.IndexOf(MCancas);

            this.Width =200;
            this.Height = 40;
            MCancas.Offset = new Point(200*(n-1),0-MCancas.ActualHeight+40);
            //Canvas.SetLeft(this, 0.0);
            //Canvas.SetTop(this, MCancas.ActualHeight-40);

           

        }


    }
}
